Node.js Push方法结果异常:Alexa Skill Lambda代码求助
Hey John, let’s break down the two issues you’re facing with your Lambda code—one related to how JavaScript handles objects, and another about your return format.
Why Your push() Result Isn’t What You Expect
The core problem here is how JavaScript treats objects: they’re reference types. When you push model2 into result.endpoints, you aren’t adding a copy of the object—you’re adding a pointer to the exact same object stored in memory.
Let’s walk through your code step by step to see what happens:
- You create
model2 = {"endpointid": "stuff"} - You update
model2.endpointidto"switch1"and push this reference into the array - Then you change
model2.endpointidto"switch2"—this modifies the same object that’s already in your array - You push that same reference again into the array
By the end, both entries in result.endpoints point to the same model2 object, so both show "endpointid": "switch2".
Fixing the Array Behavior
To get two unique endpoints in your array, you need to create a new object each time you add an entry. Here are two straightforward ways to do this:
Option 1: Create a fresh object for each endpoint
Instead of reusing model2, initialize a new object every time you need a new endpoint:
exports.handler = function(event, context) { // Initialize your base model var model1 = {"endpoints": []}; // Create first endpoint and add to array var switch1 = {"endpointid": "switch1"}; model1.endpoints.push(switch1); // Create second endpoint and add to array var switch2 = {"endpointid": "switch2"}; model1.endpoints.push(switch2); var json = JSON.stringify(model1); context.succeed({json}); };
Option 2: Copy the base object before modifying
If you want to reuse a base endpoint structure, use the spread operator (...) to make a shallow copy of the object before updating its properties:
exports.handler = function(event, context) { var model1 = {"endpoints": []}; // Base endpoint template var baseEndpoint = {"endpointid": "stuff"}; // Make a copy, update, push var switch1 = {...baseEndpoint}; switch1.endpointid = "switch1"; model1.endpoints.push(switch1); // Make another copy, update, push var switch2 = {...baseEndpoint}; switch2.endpointid = "switch2"; model1.endpoints.push(switch2); var json = JSON.stringify(model1); context.succeed({json}); };
Fixing Your Return Format
You mentioned your result comes back as { "json": "{..." }—that’s because you’re wrapping the stringified JSON in an object with a json key.
If you want to return the actual JSON object directly (which is usually what Alexa Skills expect), you can skip JSON.stringify and pass model1 straight to context.succeed:
context.succeed(model1);
This will return a clean JSON structure like:
{ "endpoints": [ {"endpointid": "switch1"}, {"endpointid": "switch2"} ] }
If you specifically need to return a stringified JSON inside the json key (for a specific Skill requirement), then your original approach is fine—just double-check that’s what the Alexa service expects.
内容的提问来源于stack exchange,提问作者John

